#include <QApplication>
#include <QDebug>
#include <QLoggingCategory>
#include <iostream>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#endif

int main(int argc, char *argv[])
{
    try {
        std::cout << "Starting test application..." << std::endl;
        
        QApplication a(argc, argv);
        
#ifdef _WIN32
        // 在Windows下分配控制台并重定向输出
        if (AllocConsole()) {
            freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
            freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
            freopen_s((FILE**)stdin, "CONIN$", "r", stdin);
            
            // 设置控制台标题
            SetConsoleTitle(L"Test Debug Console");
            
            // 启用Qt日志输出到控制台
            qSetMessagePattern("[%{time hh:mm:ss.zzz}] %{type}: %{message}");
        }
#endif
        
        qDebug() << "QApplication created successfully";
        std::cout << "QApplication created successfully" << std::endl;
        
        qDebug() << "Test completed - exiting";
        std::cout << "Test completed - exiting" << std::endl;
        
        return 0;
    } catch (const std::exception& e) {
        std::cout << "Exception caught in test main:" << e.what() << std::endl;
        qDebug() << "Exception caught in test main:" << e.what();
        return -1;
    } catch (...) {
        std::cout << "Unknown exception caught in test main" << std::endl;
        qDebug() << "Unknown exception caught in test main";
        return -1;
    }
}